//
//  HSMessageViewModel.swift
//  Home_School_08
//
//  Created by 张晓林 on 2016/12/30.
//  Copyright © 2016年 张晓林. All rights reserved.
//

import UIKit

class HSMessageViewModel {
    // 当前的消息模型
    var message: HSMessageModel {
        didSet {
            self.attributedLikeStr = getLikeNameWithArrabute()
            setReplayListAttributeStr()
        }
    }
    // 点赞的富文本属性
    var attributedLikeStr: NSAttributedString?
    // 回复列表类容的富文本属性数组
    var replayListAttributes = [NSAttributedString]()
    // 回复视图的高度
    var replayListViewH: CGFloat = 100
    
    init(model: HSMessageModel) {
        self.message = model
        self.attributedLikeStr = getLikeNameWithArrabute()
        
        setReplayListAttributeStr()
    }
    
    // 设置富文本，同计算内容的高度
    func setReplayListAttributeStr() {
        // 创建一个记录总高度的变量属性
        var rlViewH: CGFloat = 0
        
        // 计算背景视图的宽度
        let rlBgViewW = screenW - 3 * magin - messageIconW
        // 计算文本高度
        let maxSize = CGSize(width: rlBgViewW - magin * 2, height: CGFloat(MAXFLOAT))
        
        // 判断是否需要计算点赞文本高度
        if message.likenum > 0 {
            let likeStrH = attributedLikeStr!.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
            rlViewH += likeStrH + magin + 5
        }
        // 先获得回复列表数组
        let replayListArr = message.replaylist as! [HSReplayListModel]
        // 创建一个保存富文本的数组
        var attributeArr = [NSAttributedString]()
        // 循环转换
        for rlModel in replayListArr {
            // 将用户名转换为富文本
            let nameAttribute = NSMutableAttributedString(string: rlModel.int_name ?? "", attributes: [NSForegroundColorAttributeName:UIColor.colorWithHex(hexColor: 0x00a0e9),NSFontAttributeName:rlContentTextFont])
            let contentStr = ": " + (rlModel.int_content ?? "")
            let contentAttribute = NSAttributedString(string: contentStr, attributes: [NSForegroundColorAttributeName:UIColor.colorWithHex(hexColor: 0x808080),NSFontAttributeName:rlContentTextFont])
            nameAttribute.append(contentAttribute)
            // 添加到数组
            attributeArr.append(nameAttribute)
            
            // 计算回复类容高度
            let contentStrH = nameAttribute.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, context: nil).size.height
            
            rlViewH += contentStrH + 5
        }
        
        
        // 添加点赞人名和内容之间的间距
        if attributeArr.count > 0 {
            rlViewH += 5
        }
        
        // 赋值给属性
        self.replayListAttributes = attributeArr
        self.replayListViewH = rlViewH
    }
    
    // 将点赞人名字符串转换为带点赞图标的富文本字符串
    func getLikeNameWithArrabute() -> NSAttributedString? {
        
        // 先将图片转换为富文本对象
        let attachment = NSTextAttachment()
        attachment.image = UIImage(named: "notifi_support_press")
        attachment.bounds = CGRect(x: 0, y: -2, width: 12, height: 12)
        let imgAttribute = NSAttributedString(attachment: attachment)
        // 判断当前点赞字符串是否有值
        if message.likenum == 0 {
            return nil
        }
        // 获得点赞字符串
        let likeStr = " " + (message.likelist ?? "")
        let likeAtt = NSAttributedString(string: likeStr)
        let likeNewAttribute = NSMutableAttributedString(attributedString: imgAttribute)
        likeNewAttribute.append(likeAtt)
        
        
        return likeNewAttribute
    }
    
    
    
    
    
    
}
